package com.project.service.impl;

import com.project.bean.LogUserBean;
import com.project.bean.UserBean;
import com.project.mapper.LogMapper;
import com.project.mapper.UserMapper;
import com.project.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalTime;
import java.util.List;

@Service
public class UserServiceImpl implements IUserService {
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private LogMapper logMapper;

    @Override
    public UserBean login(String name, String pwd) {
        UserBean user = userMapper.login(name,pwd);
        if (user != null) {
            String time = (LocalTime.now()+"").substring(0,5);
            String info = "用户"+user.getTrueName()+" 在 "+ time +" 登录系统";
            logMapper.addLog(new LogUserBean(info));
            return user;
        }

        return null;
    }

    @Override
    public boolean addUser(UserBean user) {
      int num =  userMapper.checkUser(user.getUserName());
      if(num != 0){
          return false;
      }
      userMapper.addUser(user);
      return true;
    }

    @Override
    public void delUser(int id) {
        userMapper.delUser(id);
    }

    @Override
    public void updateLevel(int id, String grade) {
        userMapper.updateLevel(id,grade);
    }

    @Override
    public List<UserBean> findByItem(String level) {
        return userMapper.findByItem(level);
    }

    @Override
    public void updatePwd(int id, String pwd) {
        userMapper.updatePwd(id,pwd);
    }

    @Override
    public UserBean findById(int id) {
        return userMapper.findById(id);
    }
}
